<?php
/* --------------------------------------------------------------
   PropertiesControl.php 2011-07-21 gambio
   Gambio GmbH
   http://www.gambio.de
   Copyright (c) 2011 Gambio GmbH
   Released under the GNU General Public License (Version 2)
   [http://www.gnu.org/licenses/gpl-2.0.html]
   --------------------------------------------------------------
*/


class PropertiesControl 
{
	var $v_id_seperator = 'x';
	
	function PropertiesControl() 
	{
	}
	
	function get_combis_id_by_value_ids_array($p_products_id, $p_value_ids_array)
	{
		$t_accepted_combis_id = 0;
		
		$c_products_id = (int)$p_products_id;
		$t_language_id = $_SESSION['languages_id'];
		
		$coo_properties_data = MainFactory::create_object('ProductPropertiesData', array($c_products_id, $t_language_id));
		$t_combis_array = $coo_properties_data->get_properties_combis_array();
		
		foreach($t_combis_array as $t_combis_item_array)
		{
			foreach($p_value_ids_array as $t_values_id)
			{
				if(in_array($t_values_id, $t_combis_item_array['values_ids_array'] )) {
					#accept this current combis_id as long as all ids are found
					$t_accepted_combis_id = $t_combis_item_array['products_properties_combis_id'];
				} else {
					$t_accepted_combis_id = 0;
					break;
				}
				
			}
			if($t_accepted_combis_id != 0) break; #stop searching if found accepted combis_id
		}
		return $t_accepted_combis_id;
	}
	
	
	function get_orders_products_properties($p_orders_products_id)
	{
		$t_properties_output_array = array();
		
		$coo_data_object_group = new GMDataObjectGroup('orders_products_properties', array('orders_products_id' => $p_orders_products_id));
		$t_data_object_array = $coo_data_object_group->get_data_objects_array();
		
		foreach($t_data_object_array as $t_data_object_item)
		{
			$t_properties_output_array[] = array(
												'orders_products_properties_id' => $t_data_object_item->get_data_value('orders_products_properties_id'),
												'orders_products_id' 			=> $t_data_object_item->get_data_value('orders_products_id'),
												'properties_name' 				=> $t_data_object_item->get_data_value('properties_name'),
												'values_name' 					=> $t_data_object_item->get_data_value('values_name'),
												'properties_price_type'	 		=> $t_data_object_item->get_data_value('properties_price_type'),
												'properties_price' 				=> $t_data_object_item->get_data_value('properties_price')
											);
		}
		return $t_properties_output_array;
	}
	
	function get_properties_combis_price($p_properties_combis_id)
	{
		$t_output_price = 0;
		
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $p_properties_combis_id));
		
		$t_products_id 		= $coo_data_object->get_data_value('products_id');
		$t_combi_price 		= $coo_data_object->get_data_value('combi_price');
		$t_combi_price_type = $coo_data_object->get_data_value('combi_price_type');

		# use 'plus' as default if price_type is empty
		if($t_combi_price_type == '') $t_combi_price_type = 'plus';

		switch($t_combi_price_type)
		{
			case 'plus':
				$t_output_price = $t_combi_price;
				break;
				
			case 'minus':
				$t_output_price = $t_combi_price * -1;
				break;
				
			case 'fix':
				#TODO: calculate new product price
				$t_output_price = $t_combi_price;
				break;
				
			default:
				trigger_error('unknown t_combi_price_type: '. $t_combi_price_type, E_USER_ERROR);
		}
		return $t_output_price;
	}
	
	function get_properties_combis_details($p_properties_combis_id, $p_language_id)
	{
		$c_properties_combis_id = (int)$p_properties_combis_id;
		$c_language_id = (int)$p_language_id;
		
		#check parameters
		if($c_properties_combis_id == 0) trigger_error('combis_id=0', E_USER_ERROR);
		if($c_language_id == 0) trigger_error('language_id=0', E_USER_ERROR);
		
		$coo_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_details_array = $coo_data_agent->get_properties_combis_details($c_properties_combis_id, $c_language_id);
		
		#check result arrays
		//if(sizeof($t_details_array) == 0) trigger_error('sizeof($t_details_array)=0 pid-lid'. $c_properties_combis_id .'-'. $c_language_id, E_USER_ERROR);
		if(sizeof($t_details_array) == 0) 
		{
			# given combis_id doesnt exist. clean up!
			$coo_control = MainFactory::create_object('PropertiesCombisAdminControl');
			$coo_control->clear_baskets_combis($c_properties_combis_id);
		}
		return $t_details_array;
	}
	
	function get_properties_combis_quantity($p_properties_combis_id)
	{
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $p_properties_combis_id));
		$t_quantity = $coo_data_object->get_data_value('combi_quantity');
		return $t_quantity;
	}
	
	function get_properties_combis_shipping_time($p_properties_combis_id)
	{
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $p_properties_combis_id));
		$t_shipping_time_id = $coo_data_object->get_data_value('combi_shipping_status_id');
		
		$coo_data_object = new GMDataObject('shipping_status', array('shipping_status_id' => $t_shipping_time_id, 'language_id' => $_SESSION['languages_id']));
		$t_shipping_time_value = $coo_data_object->get_data_value('shipping_status_name');
		
		return $t_shipping_time_value;
	}
	
	function get_properties_combis_weight($p_properties_combis_id)
	{
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $p_properties_combis_id));
		$t_weight = $coo_data_object->get_data_value('combi_weight');
		
		return $t_weight;
	}
	
	function get_properties_combis_model($p_properties_combis_id)
	{
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $p_properties_combis_id));
		$t_model = $coo_data_object->get_data_value('combi_model');
		
		return $t_model;
	}	
	
	function add_properties_combi_to_orders_product($p_properties_combis_id, $p_orders_products_id)
	{
		$t_output_counter = 0; #count of added properties
		
		$c_properties_combis_id = (int)$p_properties_combis_id;
		$c_orders_products_id = (int)$p_orders_products_id;
		
		#check parameters
		if($c_properties_combis_id == 0) trigger_error('combis_id=0 c-p '. $c_properties_combis_id .'-'. $p_orders_products_id, E_USER_ERROR);
		if($c_orders_products_id == 0) trigger_error('orders_products_id=0 c-p '. $c_orders_products_id .'-'. $p_orders_products_id, E_USER_ERROR);
		
		$coo_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_details_array = $coo_data_agent->get_properties_combis_details($c_properties_combis_id, $_SESSION['languages_id']);
		
		#check result arrays
		if(sizeof($t_details_array) == 0) trigger_error('sizeof($t_details_array)=0 cid-pid'. $p_properties_combis_id .'-'. $p_orders_products_id, E_USER_ERROR);

		# data object for combis_model
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $c_properties_combis_id));
		$t_combi_model = $coo_data_object->get_data_value('combi_model');

		if($t_combi_model != '')
		{
			# data object for updating products_model
			$coo_data_object = new GMDataObject('orders_products', array('orders_products_id' => $c_orders_products_id));
			$t_products_model = $coo_data_object->get_data_value('products_model');
			
			if(APPEND_PROPERTIES_MODEL == "true" && $t_products_model != '') {
				$t_products_model = $t_products_model .'-'. $t_combi_model;
			}else{
				$t_products_model = $t_combi_model;
			}
			$coo_data_object->set_data_value('products_model', $t_products_model);
			$coo_data_object->save_body_data();
		}

		# data object for inserts
		$coo_data_object = new GMDataObject('orders_products_properties');
		
		foreach($t_details_array as $t_item_array)
		{
			# reset data object for insert
			$coo_data_object->set_keys(array('orders_products_properties_id' => false));
			
			$coo_data_object->set_data_value('orders_products_id', $c_orders_products_id);
			$coo_data_object->set_data_value('properties_name', $t_item_array['properties_name']);
			$coo_data_object->set_data_value('values_name', $t_item_array['values_name']);
			# TODO:
			$coo_data_object->set_data_value('properties_price_type', '');
			$coo_data_object->set_data_value('properties_price', '');

			# VARIO
			$coo_data_object->set_data_value('products_properties_combis_id', $c_properties_combis_id);

			# do insert
			$coo_data_object->save_body_data();
			$t_output_counter++;
		}
		return $t_output_counter;
	}	

	# GM_MOD: ratscheu-fix
	function change_combis_quantity($p_properties_combis_id, $p_quantity_change)
	{
		$coo_data_object = new GMDataObject('products_properties_combis', array('products_properties_combis_id' => $p_properties_combis_id));

		$t_old_qty = $coo_data_object->get_data_value('combi_quantity');
		$t_new_qty = $t_old_qty + $p_quantity_change;
		
		$coo_data_object->set_data_value('combi_quantity', $t_new_qty);
		$coo_data_object->save_body_data();
		return $t_new_qty;
	}
	
	function get_id_seperator()
	{
		return $this->v_id_seperator;
	}
	
	# compose products_id and combis_id for customers_basket-table and order-object
	function get_baskets_products_id($p_products_id, $p_combis_id)
	{
		# sample: 1{2}6{1}3x56
		$t_output_id = $p_products_id . $this->v_id_seperator . $p_combis_id;
		
		$t_output_id = strip_tags($t_output_id);
		return $t_output_id;
	}
	
	function extract_combis_id($p_baskets_products_id)
	{
		$t_output_combis_id = '';
		
		# sample: 1{2}6{1}3x56
 		$t_parts_array = explode($this->v_id_seperator, $p_baskets_products_id);
 		if(sizeof($t_parts_array) > 1) $t_output_combis_id = $t_parts_array[1];
 		
 		return $t_output_combis_id; #empty, if no id found
	}
	
	function extract_products_id($p_baskets_products_id)
	{
		# sample: 1{2}6{1}3x56
 		$t_parts_array = explode($this->v_id_seperator, $p_baskets_products_id);
 		$t_cleared_output = $t_parts_array[0];
		
 		return $t_cleared_output;
	}

	# DEPRECATED
	function clear_baskets_products_id($p_baskets_products_id)
	{
 		$t_products_id = $this->extract_products_id($p_baskets_products_id);
 		return $t_products_id;
	}
	
	
	
	
	
	
	function get_shop_languages_data()
	{
		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		$t_languages_array = $coo_properties_data_agent->get_shop_languages_data();
		
		return $t_languages_array;
	}
	
	
	function get_products_properties_data($p_products_id, $p_language_id)
	{
		$coo_properties_data = MainFactory::create_object('ProductPropertiesData', array($p_products_id, $p_language_id));
		$t_properties_data_array = $coo_properties_data->get_properties_struct();
		
		return $t_properties_data_array;
	}

	
	function get_products_properties_combis_data($p_products_id, $p_language_id)
	{
		$c_products_id = (int)$p_products_id;
		$c_language_id = (int)$p_language_id;
		
		$coo_properties_data_agent = MainFactory::create_object('PropertiesDataAgent');
		
		# get all product-related combis
		$t_properties_combis_ids_array = $coo_properties_data_agent->get_all_properties_combis_ids($c_products_id);
		
		# collection of full combis_values information
		$t_combis_full_struct_array = array();
		
		# collect full_structs
		foreach($t_properties_combis_ids_array as $t_combis_id)
		{
			$t_combis_full_struct_array[] = $coo_properties_data_agent->get_combis_full_struct($t_combis_id, $c_language_id);
		}
		
		//echo '<pre>'. print_r($t_combis_full_struct_array, true) .'</pre>';
		
		return $t_combis_full_struct_array;
	}
    
    function get_use_properties_combis_quantity($p_products_id){
		
		$t_products_id = (int)$p_products_id;
        
        $coo_data_object = new GMDataObject('products', array('products_id' => $t_products_id));
        
		$t_quantity = $coo_data_object->get_data_value('use_properties_combis_quantity');
        
		return $t_quantity;
	}
	
}

?>